﻿using System.Collections.Generic;
using System.Linq;
using ZHDJ.Data.Models.Box;
using ZHDJ.WebPage;
using System.Data.Entity;

namespace AppBox.BLL
{
    public class MenuHelper
    {
        private static List<Menu> _menus;
        private static List<Menu> _branchMenus;
        private static List<Menu> _personalMenus;
        private static List<Menu> _massMenus;
        private static List<Menu> _buzhangMenus;
        public static List<Menu> Menus
        {
            get
            {
                if (_menus == null)
                {
                    InitMenus();
                }
                return _menus;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="menuId"></param>
        /// <param name="pageFlag"> 2 组织中心 3个人中心 4;注册用户</param>
        /// <returns></returns>
        public static List<Menu> MenusByID(string menuId, int pageFlag)
        {
            InitMenus(menuId, pageFlag);
            switch (pageFlag)
            {
                case 2:
                    return _branchMenus;
                case 3:
                    return _personalMenus;
                case 4:
                    return _massMenus;
                case 5:
                    return _buzhangMenus;
            }
            return new List<Menu>();
        }

        public static void Reload()
        {
            _menus = null;
        }

        private static void InitMenus()
        {
            _menus = new List<Menu>();
            List<Menu> dbMenus = RolePageBase.DB.Menus.Include(m => m.ViewPower).OrderBy(m => m.SortIndex).ToList();
            ResolveMenuCollection(dbMenus, null, 0);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pageFlag">2 组织中心 3个人中心 4;注册用户</param>
        private static void InitMenus(string id,int pageFlag)
        {
            switch (pageFlag)
            {
                case 2:
                    _branchMenus = new List<Menu>();
                    break;
                case 3:
                    _personalMenus = new List<Menu>();
                    break;
                case 4:
                    _massMenus = new List<Menu>();
                    break;
                case 5:
                    _buzhangMenus = new List<Menu>();
                    break;
            }
            
            List<Menu> dbMenus =
                    RolePageBase.DB.Menus.Include(m => m.ViewPower)
                        .OrderBy(m => m.SortIndex)
                        .ToList();

            ResolveMenuCollection(dbMenus, null, 0, pageFlag, id);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbMenus"></param>
        /// <param name="parentMenu"></param>
        /// <param name="level"></param>
        /// <param name="pageFlag"> 2 组织中心 3个人中心 4;注册用户</param>
        /// <param name="menuId"></param>
        /// <returns></returns>
        private static int ResolveMenuCollection(List<Menu> dbMenus, Menu parentMenu, int level, int pageFlag, string menuId)
        {
            int count = 0;
           
            foreach (var menu in dbMenus.Where(m => m.Parent == parentMenu))
            {
                if (parentMenu == null && menu.ID != menuId)
                {
                    continue;
                }
                count++;
                switch (pageFlag)
                {
                    case 2:
                        _branchMenus.Add(menu);
                        break;
                    case 3:
                        _personalMenus.Add(menu);
                        break;
                    case 4:
                        _massMenus.Add(menu);
                        break;
                    case 5:
                        _buzhangMenus.Add(menu);
                        break;
                }

                menu.TreeLevel = level;
                menu.IsTreeLeaf = true;
                menu.Enabled = true;

                level++;
                int childCount = ResolveMenuCollection(dbMenus, menu, level, pageFlag, menuId);
                if (childCount != 0)
                {
                    menu.IsTreeLeaf = false;
                }
                level--;
            }

            return count;
        }
        private static int ResolveMenuCollection(List<Menu> dbMenus, Menu parentMenu, int level)
        {
            int count = 0;

            foreach (var menu in dbMenus.Where(m => m.Parent == parentMenu))
            {
                count++;

                _menus.Add(menu);
                menu.TreeLevel = level;
                menu.IsTreeLeaf = true;
                menu.Enabled = true;

                level++;
                int childCount = ResolveMenuCollection(dbMenus, menu, level);
                if (childCount != 0)
                {
                    menu.IsTreeLeaf = false;
                }
                level--;
            }

            return count;
        }
    }
}
